package com.student.management.root.service.impl;

import com.student.management.root.bean.Menu;
import com.student.management.root.bean.Role;
import com.student.management.root.bean.Users;
import com.student.management.root.dao.RoleDao;
import com.student.management.root.dao.UsersDao;
import com.student.management.root.dao.impl.RoleDaoImpl;
import com.student.management.root.dao.impl.UsersDaoImpl;
import com.student.management.root.service.UsersService;

import java.util.ArrayList;
import java.util.List;

public class UsersServiceImpl implements UsersService {
    private UsersDao usersDao =  new UsersDaoImpl();
    private RoleDao roleDao=new RoleDaoImpl();

    @Override
    public Users login(String username, String password) {
        Users login = usersDao.login(username, password);
        if (login == null) {
            return null;
        }
        Integer roleId = login.getRoleId();
        Role findbyid = roleDao.findbyid(roleId);
        List<Menu> menuList = findbyid.getMenuList();
        System.out.println("id对应菜单列表："+menuList.toString());
        ArrayList<Menu> newMenuList = new ArrayList<>();
        for (Menu menu :
                menuList) {
            if (0 == menu.getUpmenuid()) {
                List<Menu> secondList = new ArrayList<>();
                for (Menu second :
                        menuList) {
                    if (menu.getMenuid() == second.getUpmenuid()) {
                        secondList.add(second);
                    }
                }
                menu.setSecondMenuList(secondList);
                newMenuList.add(menu);
            }
        }
        System.out.println("roleId为："+roleId+"角色对应权限"+newMenuList);
        findbyid.setMenuList(newMenuList);
        login.setRole(findbyid);

        return login;
    }

    @Override
    public List<Users> getUsersList(int pageIndex, int pageSize) {
        return usersDao.getUsersList(pageIndex, pageSize);
    }

    @Override
    public int total() {
        return usersDao.total();
    }

    @Override
    public int insertUser(Users user) {
        return usersDao.insertUser(user);
    }

    @Override
    public int updateUser(Users user) {
        return usersDao.updateUser(user);
    }

    @Override
    public Users findById(int userId) {
        return usersDao.findById(userId);
    }

    @Override
    public int deleteById(Integer userId) {
        return usersDao.deleteById(userId);
    }
}
